<template>
    <div style="min-width: 809px">
        <div v-if="$route.path == '/cardOpeningList'">
            <div class="vs-search-box" style="padding: 24px 22px">
                <el-form ref="form" v-search-box-enter :inline="true" :model="paging" label-width="90px">
                    <el-form-item label="开卡日期：">
                        <el-date-picker v-model="paging.start_time" class="vs-w-250" type="daterange"
                            value-format="yyyy-MM-dd" range-separator="至" start-placeholder="开始日期"
                            end-placeholder="结束日期"> </el-date-picker>
                    </el-form-item>
                    <el-form-item label="会员等级:">
                        <el-select v-model="paging.vip_id" placeholder="请选择" class="vs-w-150" clearable>
                            <el-option v-for="item in vipList" :key="item.id" :label="item.title" :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="状态:">
                        <el-select v-model="paging.status" placeholder="请选择" class="vs-w-150" clearable>
                            <el-option label="正常" :value="1"> </el-option>
                            <el-option label="已作废" :value="0"> </el-option>
                            <el-option label="已退卡" :value="-1"> </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="">
                        <div style="width: 400px">
                            <el-input placeholder="请输入姓名或手机号或会员卡号" v-model="paging.keys">
                                <i slot="prefix" class="el-input__icon el-icon-search"></i>
                                <template slot="append">
                                    <div style="cursor: pointer" @click="handleCurrentChange(1)">查找会员</div>
                                </template>
                            </el-input>
                        </div>
                    </el-form-item>
                    <el-form-item class="">
                        <el-button @click="reset">重置</el-button>
                        <el-button @click="handleCurrentChange(1)" type="primary">查询</el-button>
                    </el-form-item>
                </el-form>
            </div>
            <div class="vs-table-box">
                <div class="operation-box clear-float">
                    <el-button icon="el-icon-plus" @click="$router.push('/addMember2')" type="primary">新增</el-button>
                    <el-button v-if="utils.getIsShopowner()" @click="exportFn" icon="el-icon-download">导出</el-button>
                </div>
                <el-table :data="tableData" class="vs-max-height" height="700px" border>
                    <el-table-column prop="id" label="单据号" min-width="90" show-overflow-tooltip align="center">
                    </el-table-column>
                    <el-table-column prop="realname" label="姓名" min-width="90" show-overflow-tooltip align="center">
                    </el-table-column>
                    <el-table-column label="性别" align="center">
                        <template slot-scope="scope">
                            {{ scope.row.gender | typeGender }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="phone" label="手机号" width="110" align="center">
                        <template slot-scope="scope">
                            {{ utils.hidePhoneNum(scope.row.phone) }}
                        </template>
                    </el-table-column>
                    <el-table-column label="会员等级" prop="vip_id" width="120" align="center">
                        <template slot-scope="scope">
                            {{ rtVip(scope.row.vip_id) }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="create_user" label="操作员工" min-width="120" align="center"> </el-table-column>
                    <el-table-column prop="vip_time" label="开卡日期" align="center" width="120">
                        <template slot-scope="{ row }" v-if="row.vip_time">
                            {{ $moment(row.vip_time * 1000).format('YYYY-MM-DD') }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="status" label="状态" width="110" align="center">
                        <template slot-scope="{ row }">
                            {{ row.status | cardOpeningStatus }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="source" label="单据来源" width="110" align="center">
                        <template slot-scope="{ row }">
                            {{ row.source | sourceTxt }}
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" align="center" width="140" fixed="right">
                        <template slot-scope="scope">
                            <span class="tab-table-text" @click="setRow(1, scope.row)">会员详情</span>
                            <span v-if="scope.row.status == 1" class="tab-red-text"
                                @click="setRow(2, scope.row)">作废</span>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="vs-paging-box">
                    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                        :current-page.sync="paging.page" :page-size="paging.limit" :page-sizes="[100, 200, 500, 1000]"
                        layout="total, sizes, prev, pager, next, jumper" :total="paging.total"> </el-pagination>
                </div>
            </div>
        </div>
        <router-view></router-view>
    </div>
</template>

<script>
export default {
    data() {
        return {
            value: '',
            recordList: [],
            loading: false,
            loading2: false,
            isBtn: false,
            dialogFormVisible3: false,
            dialogFormVisible: false,
            dialogVisible: false,
            props: {
                lazy: true,
                lazyLoad: async (node, resolve) => {
                    const { value } = node;
                    console.log(node, 'node');
                    let { code, data = [] } = await this.$Http.tree({ pid: value });
                    data.forEach((v) => {
                        v.label = v.area_name;
                        v.value = v.id;
                        v.leaf = v.level >= 4;
                    });
                    // 通过调用resolve将子节点数据返回，通知组件数据加载完成
                    resolve(data);
                },
            },
            form: {
                user_id: '',
                pay_type: '',
                price: '',
                staff_id: '',
            },
            keys: '',
            paging: {
                page: 1,
                total: 0,
                limit: 100,
                keys: "",//会员卡号、姓名、手机号
                start_time: "",
                end_time: "",
                vip_id: "",
                status: ""
            },
            storeList: [],
            tableData: [
                {
                    username: '111',
                    code: '111',
                    create_time: '111',
                },
            ],
            ruleFrom1: {
                id: '',
                return_money: '',
            },
            computedOptions: [],
            return_money: '',
            vipList: [],
            data: [{}],
            staffList: [],
            region: [], //地址列表
        };
    },
    created() {
        this.getVip();
        if (this.$route.path == '/cardOpeningList') {
            this.repairGetList();
        }
    },
    watch: {
        value(old, value) {
            console.log(old, 'old');
            console.log(value, 'value');
            if (!old.length) {
                this.paging.town_id = '';
            }
        },
    },
    filters: {
        cardOpeningStatus(val) {//状态 1:正常 0:已作废 -1: 已退卡
            switch (Number(val)) {
                case 0:
                    val = '已作废';
                    break;
                case 1:
                    val = '正常';
                    break;
                case -1:
                    val = '已退卡';
                    break;
            }
            return val;
        },
    },
    methods: {
        // 获取地址列表
        async getAddressList() {
            let { code, data } = await this.$Http.tree({ pid: this.paging.area_id });
            if (code) {
                this.region = data || [];
            }
        },
        chioseAddress(e) {
            this.paging.province_id = e[0];
            this.paging.city_id = e[1];
            this.paging.area_id = e[2];
            if (this.paging.area_id) {
                this.getAddressList();
            }
        },
        // 导出
        async exportFn() {
            let url = this.utils.data.baseURL + 'store/user/cardOpenListExport';
            this.utils.urlToBlob(url, '开卡列表', this.paging);
        },
        rtVip(id) {
            let name = '';
            if (this.vipList.length) {
                this.vipList.forEach((e) => {
                    if (e.id == id) {
                        name = e.title;
                    }
                });
                return name || id;
            } else {
                return id;
            }
        },
        async getVip() {
            let { code, data } = await this.$Http.getVip();
            if (code === 10000) {
                console.log(data);
                let obj = {
                    id: 0,
                    title: '散客',
                };
                this.vipList = [obj, ...data];
            }
        },
        async confirm() {
            //退卡
            if (!this.ruleFrom1.return_money) {
                this.$message.error('请输入退卡金额');
                return;
            } else {
                if (this.ruleFrom1.return_money > this.return_money) {
                    this.$message.error('请输入退卡金额不能大于余额');
                    return;
                }
            }
            let { code, data } = await this.$Http.returnVip(this.ruleFrom1);
            if (code) {
                this.$message.success('退卡成功');
                this.dialogFormVisible3 = false;
                this.handleCurrentChange(1);
            }
        },
        async recharges() {
            if (!this.form.price) {
                this.$message.error('请输入重置金额');
                return;
            }
            if (!this.form.staff_id) {
                this.$message.error('请选择员工');
                return;
            }
            if (!this.form.pay_type) {
                this.$message.error('请选择支付方式');
                return;
            }
            let { code, data } = await this.$Http.setUserRecharge(this.form);
            if (code) {
                this.$message.success('充值成功');
                this.handleCurrentChange(1);
                this.dialogFormVisible = false;
                this.form = this.$options.data().form;
                //   this.paging = this.$options.data().paging;
            }
        },
        async setRow(type, row) {
            console.log(row, 'dddd');
            if (type == 1) {
                this.utils.goUrl(`/memberInfo?id=${row.id}`);
            } else if (type == 2) {
                this.$msgbox({
                    title: '提示',
                    message: '确定要作废该用户吗?',
                    type: 'warning',
                    showCancelButton: true,
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    beforeClose: async (action, instance, done) => {
                        if (action === 'confirm') {
                            instance.confirmButtonLoading = true;
                            instance.confirmButtonText = '执行中...';
                            let { code, data } = await this.$Http.vipVoid({ user_id: row.id })
                            if (code) {
                                this.$message.success('作废成功!');
                                this.handleCurrentChange(1)
                            }
                            done();
                            instance.confirmButtonLoading = false;
                        } else {
                            done();
                        }
                    }
                }).then(action => {

                }).catch(() => { });
            }
            return;
        },
        async repairGetList() {
            let obj = Object.assign({}, this.paging)
            if (obj.start_time?.[1]) {
                obj.end_time = obj.start_time?.[1];
                obj.start_time = obj.start_time?.[0];
            } else {
                obj.end_time = "";
                obj.start_time = "";
            }
            this.loading = true;
            let { code, data } = await this.$Http.cardOpenList(obj);
            this.loading = false;
            if (code) {
                this.tableData = data.data;
                this.paging.limit = data.per_page;
                this.paging.total = data.total;
            }
            this.$forceUpdate();
        },
        // 充值
        async recharges() {
            if (!this.form.price) {
                this.$message.error('请输入重置金额');
                return;
            }
            if (!this.form.staff_id) {
                this.$message.error('请选择员工');
                return;
            }
            if (!this.form.pay_type) {
                this.$message.error('请选择支付方式');
                return;
            }
            let { code, data } = await this.$Http.setUserRecharge(this.form);
            if (code) {
                this.$message.success('充值成功');
                this.repairGetList();
                this.dialogFormVisible = false;
                this.form = this.$options.data().form;
            }
        },
        reset() {
            //重置
            this.paging = this.$options.data().paging;
            this.form.date = '';
            this.handleCurrentChange(1);
        },
        handleSizeChange(val) {
            this.paging.limit = val;
            this.repairGetList();
        },
        handleCurrentChange(val) {
            this.paging.page = val;
            this.repairGetList();
        },
    },
};
</script>

<style lang="scss" scoped>
.image-thumbnail {
    width: 130px;
    height: 130px;
}

.vs-search-box {
    display: flex;
}

.btn_info {
    cursor: pointer;
    color: #409eff;
}
</style>
